﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 28/12/2008
// Time: 14:27
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using csharp.common;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of frmRegisterNeighborhood.
	/// </summary>
	public partial class frmRegisterNeighborhood : RegisterTemplate
	{
		private Neighborhood neighborhood = new Neighborhood();
		private Neighborhoods listNeighborhood = new Neighborhoods();
		private Cities listCity  = new Cities();
		private States listState = new States();
		private Countries listCountry = new Countries();
		
		private bool mutexCountry = false;
		private bool mutexState   = false;
		
		public frmRegisterNeighborhood()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		
		#region overrided methods
			protected override void newEvent()
			{
				neighborhood.clear();
				txtName.Focus();
			}
			
			/// <summary>
			/// Set data from edits to class
			/// </summary>
			protected override void setClass()
			{
				neighborhood.name			= txtName.Text;
				neighborhood.zipCode		= txtZipCode.Text;
				neighborhood.enable		= cbxEnable.Checked;
				neighborhood.cityId		= (cmbCity.SelectedItem as City).id;
			}
			
			/// <summary>
			/// Set data from class to edits
			/// </summary>
			protected override void setEdits()
			{
				neighborhood.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				
				if (neighborhood.get())
				{
					txtName.Text			= neighborhood.name;
					txtZipCode.Text		= neighborhood.zipCode;
					cbxEnable.Checked 	= neighborhood.enable;
					Functions.setSelectIndexComboCities(cmbCity, neighborhood.cityId);
					
					listCity.search(neighborhood.cityId);
					Functions.setSelectIndexComboStates(cmbState, listCity.get().stateId);
					
					listState.search(listCity.get().stateId);
					Functions.setSelectIndexComboCountries(cmbCountry, listState.get().countryId);
				}
			}
			
			/// <summary>
			/// Fill the DataGridView
			/// </summary>
			protected override void fillDataGridView()
			{
				int tam;
				listNeighborhood.fillList();
				
				tam = listNeighborhood.count();
				dgvMain.Rows.Clear();
				for (int i=0; i < tam; i++)
				{
					dgvMain.Rows.Add(new String[] {listNeighborhood.get(i).id, listNeighborhood.get(i).name});
				}				
			}
			
			/// <summary>
			/// Find the object of search
			/// </summary>
			protected override void find()
			{
				common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
			}		
			
		   /// <summary>
		   /// Save the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
		   protected override bool save()
		   {
		   	return neighborhood.save();
		   }
		   
		   /// <summary>
		   /// Update the register in the database
		   /// </summary>
		   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
		   protected override bool update()
		   {   
		   	return neighborhood.update();
		   }
		   
		   /// <summary>
		   /// Delete the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
		   protected override bool delete()
		   {
		   	neighborhood.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();	
		   	return neighborhood.delete();
		   }
		   
		   /// <summary>
		   /// Method call in initialization
		   /// </summary>
		   protected override void init()
		   {
		   	DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
		   	DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
		   	
		   	id.Name = "id";
		   	id.Visible = false;
		   	
		   	name.Name = "name";
		   	name.HeaderText = "Nome";
		   	
		   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id, name});
		   	
		   	listCountry.fillList();
		   	Functions.fillComboCountries(listCountry, cmbCountry,"id", "name");	
		   	
		   	listState.fillList();
		   	Functions.fillComboStates(listState, cmbState,"id", "name");
		   	
		   	listCity.fillList();
		   	Functions.fillComboCities(listCity, cmbCity, "id", "name");
		   }
		#endregion

		void CmbCountrySelectedValueChanged(object sender, EventArgs e)
		{
			if ((cmbCountry.SelectedIndex >= 0) && (!mutexCountry))
			{
				cmbState.SelectedIndex = -1;
				cmbCity.SelectedIndex  = -1;
				listState.fillList((cmbCountry.SelectedItem as Country).id);
				Functions.fillComboStates(listState, cmbState, "id", "name");
			}	
			mutexCountry= false;
		}
		
		void CmbStateSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbState.SelectedIndex >= 0)
			{
				if (mutexState)
				{
					mutexCountry= true;
					listState.search((cmbState.SelectedItem as State).id);
					Functions.setSelectIndexComboCountries(cmbCountry, listState.get().countryId);					
					mutexCountry= false;
				}
				else
				{
					cmbCity.SelectedIndex = -1;
					listCity.fillList((cmbState.SelectedItem as State).id);
					Functions.fillComboCities(listCity, cmbCity, "id", "name");
				}
			}
			mutexState= false;
		}		
		
		
		void CmbCitySelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbCity.SelectedIndex >= 0)
			{
				mutexState= true;
				listCity.search((cmbCity.SelectedItem as City).id);
				Functions.setSelectIndexComboStates(cmbState, listCity.get().stateId);
				mutexState= false;
			}
		}
	}
}
